package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.BusinessService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/business")
public class BusinessController {
    @Autowired
    BusinessService businessService;

    //分页查询和模糊查询
    @RequestMapping("/business-list")
    @ResponseBody
    public Map<String,Object> businessList(String businessTheme, String Start, String Over,
                                           String businessSqr, String businessPlace, String businessState,
                                           Integer pageNum, Integer pageSize, HttpSession session){
        Page<Object> obj = PageHelper.startPage(pageNum, pageSize);
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        String empName = null;
        if(jueNameList.indexOf("boss")>-1){
            empName = null;
        }else{
            empName = us.getEmpName();
        }
        List<Business> businessList = businessService.allbusines(businessTheme,businessState,businessSqr,businessPlace,Start,Over,empName) ;
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",businessList);
        return map;
    }

    //按id查询（详情查询）
    @RequestMapping("/business-xqlist")
    @ResponseBody
    public Business xqbusines(Integer bId){
        return businessService.xqbusines(bId);
    }

    //确认按钮（新增或修改）
    @RequestMapping("add-business")
    @ResponseBody
    public void addbusiness(@RequestBody Business business){
        businessService.addbusiness(business);
    }

    //逻辑删除
    @RequestMapping("delete-business")
    @ResponseBody
    public void deleteBusiness(int id){
        businessService.deleteBusiness(id);
    }
}
